package objects;

import java.io.File;
import java.util.Random;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author FastLDL
 */
public class Macro {
    /*server address (ip or url), port and protocol*/
    private static final String IP = "localhost";
    private static final int PORT = 8084;
    private static final String PROTOCOL = "http";
    public static final String BASE_URL = PROTOCOL+"://"+IP+":"+PORT+"/";
            
    
    /* application base virtual address */
     public static final String BASE = "/Camel-MCR/";
     
     /* set to true if within unitn */
     public static final boolean UNI = false;
     
     /* sender email address with which automatically sent emails are presented */
     public static final String SYSTEM_ADDRESS = "noReply@mcr.com";
     
     /* path used to find images */
     public static final String IMGPATH = "/img/pictures/";
     public static final String LOCAL_IMGPATH = "img"+File.separator+"pictures"+File.separator;
     
     /* path used to find images for urgency level */
     public static final String URGIMGPATH = "/img/urgency/";
     
     /* minimum lapse of time (in seconds) necessary for the patient to be
      * reminded of a medical examination (and to go to his doctor).
      * Set to 0 to mean immediatly.
      */
     public static final int MINIMUM_LAPSE = 10; // immediate
     
     /* the lapse of time (in seconds) between the moment a skipped examination
      * is aknowledged and the next date it is scheduled
      */
     public static final int LAPSE_AFTER_NOTIFICATION = 10;
     
     /*the max number of visits displayed to the patient in the appropriate tab
      */
     public static final int MAX_VISITS_NUMBER = 5;
     
     /* used to generate random strings */
     public static String generatePassword(){
         final int numberOfNumbers = 10;
         final int numberOfLetters = 26;
         final int numberOfLiterals = numberOfLetters + numberOfNumbers;
         final int lenght = 8;
         
         Random r = new Random(System.currentTimeMillis());         
         
         String psw = "";
         
         for(int i=0; i<lenght; i++){
             int c = Math.abs(r.nextInt()) % numberOfLiterals;
             
             if (c < numberOfNumbers) psw += c;
             else psw += (char)('a' + c - numberOfNumbers);
         }
         return psw;
     }


     private static HttpSession getSession(){
         FacesContext context = FacesContext.getCurrentInstance();
         ExternalContext ec = context.getExternalContext();
         
         final HttpServletRequest request = (HttpServletRequest)ec.getRequest();
         return request.getSession();
     }
     
     public static String localPath(String filePath){
         return Macro.getSession().getServletContext().getRealPath(filePath);
     }
     
     public static String virtualPath(String filePath){
         return Macro.BASE + filePath;
     }
     
     public static String url(String filePath){
         return Macro.BASE_URL+"Camel-MCR/"+filePath;
     }
}